﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellNucleus_h
#define CellNucleus_h

#include "CellPrerequisites.h"
#include "CellSingleton.h"
#include "CellNucleusConfig.h"

namespace cell
{
/** \addtogroup Core
 *  @{
 */
/** \addtogroup General
 *  @{
 */

// 标签类型定义
typedef UUID NucleusTag;
#define TAG_TO_STRING(tag) UUID_TO_STRING(tag)

/** 内核类。Cell Cloud 的核心对象定义。
@author
	Jiangwei Xu
@remarks
	细胞核类是系统的核心类，负责维护系统内所有模块。
	通过 Nucleus 可以访问到系统内所有管理对象。
*/
class _CellExport Nucleus : public Singleton<Nucleus>, public NucleusAlloc
{
public:
	/** 构造函数。
	*/
	Nucleus(const NucleusConfig& config, const String& logFileName = "cell.log");

	/** 析构函数。
	*/
	virtual ~Nucleus(void);

public:
	/** 启动内核。
	*/
	bool startup(void);

	/** 关停内核。
	*/
	void shutdown(void);

	/** 返回内核的标签。
	*/
	inline const NucleusTag& getTag(void) const { return mTag; }

	/** 返回字符串格式的内核标签。
	*/
	inline String getTagAsString(void) const { return TAG_TO_STRING(mTag); }

	/** 返回内核配置。
	*/
	inline const NucleusConfig& getConfig(void) const { return mConfig; }

	/** 返回性能收集器。
	*/
	const Performance* getPerformace(void) const;

	/** 返回云图。
	*/
	CloudGraph* getCloudGraph(void);

	/** 返回本地描述端点。
	*/
	Endpoint* getNativeEndpoint(void);

	/** 注册 Cellet 。
	*/
	void registerCellet(Cellet* cellet);

	/** 注销 Cellet 。
	*/
	void unregisterCellet(Cellet* cellet);

	/** 返回内核内的所有 Cellet 。
	*/
	const Cellets& getCellets(void) const;

	/** 返回指定识别符的 Cellet 。
	*/
	Cellet* getCellet(const String& identifier);

	/** 启动内核自旋。
	*/
	void startSpinning(void);

	/** 停止内核自旋。
	*/
	void stopSpinning(void);

protected:
	/** 加载适配器库。
	*/
	bool loadAdapter(DynLib* lib);

	/** 卸载适配器库。
	*/
	void unloadAdapter(DynLib* lib);

	/** 加载 Cellet 。
	*/
	bool loadCellet(DynLib* lib);

	/** 卸载 Cellet 。
	*/
	void unloadCellet(DynLib* lib);

private:
	/** 在当前工作目录下搜索动态库，并尝试加载。
	*/
	void loadExternalLib(void);

	/** 卸载已加载的动态库。
	*/
	void unloadExternalLib(void);

	/** 激活所有 Cellet 。
	*/
	void activateCellets(void);

	/** 沉默所有 Cellet 。
	*/
	void deactivateCellets(void);

	/**
	*/
	void createDynLibBlacklist(void);

	/**
	*/
	void destroyDynLibBlacklist(void);

	/**
	*/
	bool existDynLibBlacklist(const String& filename);

public:
	typedef std::vector<DynLib*> AdapterLibList;
	typedef std::vector<DynLib*> CelletLibList;
	typedef std::vector<String> DynLibBlacklist;

private:
	NucleusTag mTag;
	NucleusConfig mConfig;
	bool mSpinning;

	Cryptology* mCryptology;
	Performance* mPerformace;

	LoggerManager* mLoggerManager;
	DynLibManager* mDynLibManager;
	ExecutorManager* mExecutorManager;
	StorageEnumerator* mStorageEnumerator;	/// 存储器枚举
	DialectEnumerator* mDialectEnumerator;	/// 方言枚举

	TalkService* mTalkService;

	AdapterLibList mAdapterLibs;
	CelletLibList mCelletLibs;

	Cellets mCellets;
	CelletSupervisor* mCelletSupervisor;

	CloudGraph* mCloudGraph;		/// Cellet Graph
	Endpoint* mNative;

	ContactService* mContactService;

	DynLibBlacklist mDynLibBlacklist;

#ifdef CELL_CRASH_REPORT
	CrashReport* mCrashReport;
#endif // CELL_CRASH_REPORT

public:
	/** 覆盖标准 Singleton 检索方法。
	*/
	static Nucleus& getSingleton(void);

	/** 覆盖标准 Singleton 检索方法。
	*/
	static Nucleus* getSingletonPtr(void);
};

/**
*/
void _CellExport initializeSock(void);
/**
*/
void _CellExport finalizeSock(void);

/** @} */
/** @} */
} // end namespace cell

#endif // CellNucleus_h
